package com.ssm.subject.domain.service.impl;


import com.alibaba.fastjson.JSON;
import com.ssm.subject.common.enums.CategoryTypeEnum;
import com.ssm.subject.common.enums.IsDeletedFlagEnum;
import com.ssm.subject.domain.bo.SubjectCategoryBO;
import com.ssm.subject.domain.bo.SubjectLabelBO;
import com.ssm.subject.domain.convert.SubjectLabelBOConverter;
import com.ssm.subject.domain.service.SubjectLabelDomainService;
import com.ssm.subject.infra.basic.entity.SubjectCategory;
import com.ssm.subject.infra.basic.entity.SubjectLabel;
import com.ssm.subject.infra.basic.entity.SubjectMapping;
import com.ssm.subject.infra.basic.service.SubjectCategoryService;
import com.ssm.subject.infra.basic.service.SubjectLabelService;
import com.ssm.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Log4j2
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;
    /**
     * 新增标签
     * @param subjectLabelBO
     */
    @Override
    public int insert(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.add.subjectCategoryBO:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.SubjectLabelBoTOSubjectLabel(subjectLabelBO);
        return subjectLabelService.insert(subjectLabel);
    }

    /**
     * 更新标签
     * @param subjectLabelBO
     */
    @Override
    public int update(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.update.subjectCategoryBO:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.SubjectLabelBoTOSubjectLabel(subjectLabelBO);
        return subjectLabelService.update(subjectLabel);
    }

    /**
     * 删除标签
     * @param subjectLabelBO
     */
    @Override
    public int delete(SubjectLabelBO subjectLabelBO) {
        if(log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.delete.subjectCategoryBO:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.SubjectLabelBoTOSubjectLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        return subjectLabelService.update(subjectLabel);
    }

    /**
     * 1. 传入一级标签id：新增题目时，要查询当前大分类下的所有标签来进行选择
     * 2. 传入二级标签id：只显示题目关联的标签，在mapping表中先查询到标签id，再从label表查询实体
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类id为一级标签，则查询该分类下所有标签
        SubjectCategory subjectCategory = subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode() == subjectCategory.getCategoryType()) {
            SubjectLabel subjectLabel = SubjectLabelBOConverter.INSTANCE.SubjectLabelBoTOSubjectLabel(subjectLabelBO);
            subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            List<SubjectLabel> subjectLabelBOList = subjectLabelService.queryByCondtion(subjectLabel);
            List<SubjectLabelBO> subjectLabelBOList1 = SubjectLabelBOConverter.INSTANCE.LabelListToLabelBoList(subjectLabelBOList);
            return subjectLabelBOList1;
        }
        Long categoryId = subjectLabelBO.getCategoryId();
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectMapping> subjectMappingList = subjectMappingService.queryByLabelId(subjectMapping);
        //标签id为空，则直接返回空集合
        if(CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        } 
        List<Long> labelIds = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.queryByLabelIds(labelIds);
        List<SubjectLabelBO> subjectLabelBOList = new ArrayList<>();
        subjectLabelList.forEach(subjectLabel -> {
            SubjectLabelBO subjectLabelBO1 = SubjectLabelBOConverter.INSTANCE.SubjectLabelToBo(subjectLabel);
            subjectLabelBO1.setCategoryId(categoryId);
            subjectLabelBOList.add(subjectLabelBO1);
        });
        return subjectLabelBOList;
    }

}
